package com.yuandian.middle.platform.service.impl;

import com.yuandian.api.middle_platform.po.*;
import com.yuandian.api.middle_platform.vo.*;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.middle.platform.mapper.*;
import com.yuandian.middle.platform.service.IOrgAbilityOverviewService;
import com.yuandian.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * 说明：单位能力总览接口实现
 * @Author tianlr
 * @Date 2023/5/16 10:35
 */

@Service
public class OrgAbilityOverviewServiceImpl implements IOrgAbilityOverviewService {

    @Autowired
    private OrgAbilityOverviewMapper orgAbilityOverviewMapper;
    @Autowired
    private AbilityServerMapper abilityServerMapper;

    /**
     * 查询单位
     * @return
     */
    @Override
    public R selectDataOrg(OrgAbilityParam param) {
        //查询单位
        List<InternalOrgPojo> list = orgAbilityOverviewMapper.selectOrg(param != null ? param.getOrgCode() : "");
        List<InternalOrgVo> reList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            InternalOrgVo vo = null;
            for (InternalOrgPojo pojo : list) {
                vo = new InternalOrgVo();
                vo.setOrgCode(pojo.getOrgCode());
                vo.setOrgName(pojo.getOrgName());
                vo.setOrgType(pojo.getOrgType());
                vo.setValue(pojo.getOrgCode());
                vo.setLabel(pojo.getOrgName());
                reList.add(vo);
            }
        }
        return R.ok(reList);
    }

    /**
     * 作为能力提供方，查询能力数、使用方数、订购单量
     * @return
     */
    @Override
    public R selectDataByProvider(OrgAbilityParam param) {
        //查询能力数-根据单位编码查询提供的能力数
        int abilityNum = abilityServerMapper.selectAbilityCount(param.getOrgCode());

        //查询使用方
        int userNum = combineUserNumber(param.getOrgCode(), null, null);

        //查询订购单量-根据提供方编码查询订单量（内部订购单表查询）
        int purchaseOrderNum = orgAbilityOverviewMapper.selectInternalPurchaseCount(param.getOrgCode(), null, null);

        OverviewDataVo vo = new OverviewDataVo();
        vo.setOrgCode(param.getOrgCode());
        vo.setAbilityNumber(abilityNum);
        vo.setPurchaseOrderNumber(purchaseOrderNum);
        vo.setUserNumber(userNum);

        return R.ok(vo);
    }


    /**
     * 作为能力使用方，查询能力数、订购单量
     * @param param
     * @return
     */
    @Override
    public R selectDataByUser(OrgAbilityParam param) {
        //查询能力数-根据单位编码查询使用的能力数，需要将以下两类数据合并
        Set<String> set = new HashSet<>();
        //根据使用方编码查询能力编码（内部订购单表查询）
        List<String> internalList = orgAbilityOverviewMapper.selectAbilityInternalPurchaseByUser(param.getOrgCode());
        if (internalList != null && internalList.size() > 0) {
            set.addAll(internalList);
        }
        //根据使用方编码查询能力编码（使用方提供方能力关系表）
        List<String> flowList = orgAbilityOverviewMapper.selectAbilityRelationshipByUser(param.getOrgCode());
        if (flowList != null && flowList.size() > 0) {
            set.addAll(flowList);
        }

        //查询订购单量-根据使用方编码查询订单量（内部订购单表查询）
        int purchaseOrderNum = orgAbilityOverviewMapper.selectInternalPurchaseCount(null, param.getOrgCode(), null);

        OverviewDataVo vo = new OverviewDataVo();
        vo.setOrgCode(param.getOrgCode());
        vo.setAbilityNumber(set.size());
        vo.setPurchaseOrderNumber(purchaseOrderNum);

        return R.ok(vo);
    }

    /**
     * 查询能力对应的使用方数、订购单量
     * @param param
     * @param isPurchaseOrder 是否查询订购单  0：不查询   1：查询
     * @return
     */
    @Override
    public R selectDataByAbility(OrgAbilityParam param, int isPurchaseOrder) {
        //查询使用方
        int userNum = combineUserNumber(null, param.getAbilityCode(), param.getServiceCode());
        //查询订购单量
        int purchaseOrderNum = 0;
        if (isPurchaseOrder == 1) {
            //查询订购单量-根据提供方编码/使用方编码/能力编码查询订单量（内部订购单表查询）
            purchaseOrderNum = orgAbilityOverviewMapper.selectInternalPurchaseCount(null, null, param.getAbilityCode());
        }
        AbilityOverviewDataVo vo = new AbilityOverviewDataVo();
        vo.setAbilityCode(param.getAbilityCode());
        vo.setUserNumber(userNum);
        vo.setPurchaseOrderNumber(purchaseOrderNum);
        return R.ok(vo);
    }

    /**
     * 获取使用方数
     * @return
     */
    private int combineUserNumber(String orgCode, String abilityCode, String serviceCode){
        //查询使用方-需要将以下3部分数据进行合并
        //根据提供方编码查询使用方编码（内部订购单表查询）
        List<String> orgCodeList = orgAbilityOverviewMapper.selectInternalPurchase(orgCode, abilityCode, serviceCode);
        //根据提供方编码查询使用方编码（外部订购单表查询）
        List<String> externalOrgCodeList = orgAbilityOverviewMapper.selectExternalPurchase(orgCode, abilityCode);
        //根据提供方编码查询使用方编码（使用方提供方能力关系表）
        List<UserProviderRelationshipPojo> flowlist = orgAbilityOverviewMapper.selectUserProviderRelationship(orgCode, abilityCode, serviceCode);

        //存放默认单位编码
        Set<String> orgCodeSet = new HashSet<String>();
        if (orgCodeList != null && orgCodeList.size() > 0) {
            orgCodeSet.addAll(orgCodeList);
        }
        //存放一级能开单位编码
        Set<String> oneLevelOrgCodeSet = new HashSet<String>();
        if (flowlist != null && flowlist.size() > 0) {
            for (UserProviderRelationshipPojo pojo : flowlist) {
                //默认单位编码策略
                if ("1".equals(pojo.getRecognitionStrategy())) {
                    orgCodeSet.add(pojo.getFlowUserOrgCode());
                } else {
                    oneLevelOrgCodeSet.add(pojo.getFlowUserOrgCode());
                }
            }
        }
        //累加使用方数
        int userNum = orgCodeSet.size() + oneLevelOrgCodeSet.size() + (externalOrgCodeList != null ? externalOrgCodeList.size() : 0);
        return userNum;
    }
}
